package core.proto.vis.controls;

import java.awt.event.MouseEvent;

import prefuse.Display;
import prefuse.controls.ControlAdapter;
import prefuse.visual.VisualItem;
import core.util.logging.SimpleLogger;
import core.util.logging.constants.Location;

public class ToolTipControl extends ControlAdapter
{
    private static final SimpleLogger LOGGER = SimpleLogger.getLogger();

    private String[] label;

    private StringBuffer sbuf;

    /**
     * Create a new ToolTipControl.
     * 
     * @param field
     *            the field name to use for the tooltip text
     */
    public ToolTipControl( String field ) {
        this( new String[] { field } );
    }

    /**
     * Create a new ToolTipControl.
     * 
     * @param fields
     *            the field names to use for the tooltip text. The values of
     *            each field will be concatenated to form the tooltip.
     */
    public ToolTipControl( String[] fields ) {
        label = fields;
        if( fields.length > 1 )
            sbuf = new StringBuffer();
    }

    /**
     * @see prefuse.controls.Control#itemEntered(prefuse.visual.VisualItem,
     *      java.awt.event.MouseEvent)
     */
    public void itemEntered( VisualItem item, MouseEvent e ) {
        Display d = (Display) e.getSource();
        if( label.length == 1 ) {
            // optimize the simple case
            if( item.canGetString( label[0] ) )
                d.setToolTipText( item.getString( label[0] ) );
        } else {
            sbuf.delete( 0, sbuf.length() );
            for( int i = 0; i < label.length; ++i ) {
                if( item.canGetString( label[i] ) ) {
                    if( sbuf.length() > 0 )
                        sbuf.append( "; " );
                    sbuf.append( item.getString( label[i] ) );
                }
            }
            d.setToolTipText( sbuf.toString() );
            LOGGER.log( "Edge: " + sbuf.toString(), Location.VZ,
                    "inspected edge value" );
        }
    }

    /**
     * @see prefuse.controls.Control#itemExited(prefuse.visual.VisualItem,
     *      java.awt.event.MouseEvent)
     */
    public void itemExited( VisualItem item, MouseEvent e ) {
        Display d = (Display) e.getSource();
        d.setToolTipText( null );
    }

} // end of class ToolTipControl

